﻿using System.Data;
using System.Drawing;

namespace XYAC.Framework.MonitorTireMaterialOnline.DataExporter
{
    public enum NominalType
    {
        SingleSymm = 0,//"单橡胶材质"
        DoubleSymm = 1, //"双橡胶材质"
        SingleNoSymm = 2,
        SingleWidthSymm = 3//"单橡胶材质(宽度对称)"
    }
    /// <summary>
    /// 关键点分析结果
    /// </summary>
    public struct MyAlarmPoints
    {
        public PointData NominalKeyPoint;
        public PointData MeasuringKeyPoint;
        public PointData DiffKeyPoint;
    }

    public struct JugePar
    {
        public float ThickTolTol;
        public float AreaTolDown;
        public float AreaTolUp;
        public float AreaLeftRightTol;
        public float WidthTolDown;
        public float WidthTolUp;
        public Boolean AlarmType1;
        public Boolean AlarmType2;
        public Boolean AlarmType3;
        public Boolean AlarmType4;
        public Boolean AlarmType5;
        public Boolean AlarmType6;
        public Boolean AlarmType7;
        public Boolean AlarmType8;
        public Boolean AlarmType9;
        public Int32 FindKeyType;
        public Boolean chbArae;
        public Boolean chbWidth;
    }
    /// <summary>
    /// 测量结果
    /// </summary>
    public struct MeasureResult
    {
        public Boolean BResult;
        public int OkPoints;
        public int TotalPoints;
        public float FPercent;
        public int AlarmType;
        public string AlarmTypeString
        {
            get
            {
                string ret = "";
                if (clsLanguage._CurLanguage == "zh-CHS")
                {
                    if (this.BResult == true)
                    {
                        return "合格";
                    }
                    else
                    {
                        if ((this.AlarmType & 0x01) > 0) ret = "报警";
                        if (this.AlarmType > 1) ret = "不合格";
                        if ((this.AlarmType & 0x02) > 0) ret += "/单点超";
                        if ((this.AlarmType & 0x04) > 0) ret += "/多点超";
                        if ((this.AlarmType & 0x08) > 0) ret += "/百分比超";
                        if ((this.AlarmType & 0x10) > 0) ret += "/不对称";
                        if ((this.AlarmType & 0x20) > 0) ret += "/面积";
                        if ((this.AlarmType & 0x40) > 0) ret += "/肩宽";
                        if ((this.AlarmType & 0x80) > 0) ret += "/总宽";
                        if ((this.AlarmType & 0x100) > 0) ret += "/肩厚";
                        if ((this.AlarmType & 0x200) > 0) ret += "/冠厚";
                    }
                }
                else
                {
                    if (this.BResult == true)
                    {
                        return "OK";
                    }
                    else
                    {

                        if ((this.AlarmType & 0x01) > 0) ret = "Alarm";
                        if (this.AlarmType > 1) ret = "NG";
                        if ((this.AlarmType & 0x02) > 0) ret += "/Type1";
                        if ((this.AlarmType & 0x04) > 0) ret += "/Type2";
                        if ((this.AlarmType & 0x08) > 0) ret += "/Type3";
                        if ((this.AlarmType & 0x10) > 0) ret += "/Type4";
                        if ((this.AlarmType & 0x20) > 0) ret += "/Type5";
                        if ((this.AlarmType & 0x40) > 0) ret += "/Type6";
                        if ((this.AlarmType & 0x80) > 0) ret += "/Type7";
                        if ((this.AlarmType & 0x100) > 0) ret += "/Type8";
                        if ((this.AlarmType & 0x200) > 0) ret += "/Type9";
                    }
                }
                return ret;
            }
        }
    }

    public class ClsMyNominal : Nominal
    {
        public delegate void DataChangedDelegate();//MyPointData MyPD
        public event DataChangedDelegate DataChanged;
        #region "属性"
        private NominalPointDataList m_MyPointDatas;
        public NominalPointDataList MyKeyPoints
        {
            get
            {
                return m_MyPointDatas;
            }
        }
        //'轮廓类型
        protected NominalType m_NominalType;
        public NominalType NominalType
        {
            get
            {
                return m_NominalType;
            }
        }
        //轮廓类型
        public String TypeStr
        {
            get
            {
                switch (m_NominalType)
                {
                    case NominalType.DoubleSymm:
                        return "双橡胶【对称】材质";
                    case NominalType.SingleSymm:
                        return "单橡胶【对称】材质";
                    case NominalType.SingleNoSymm:
                        return "单橡胶【不对称】材质";
                    default:
                        return "未知";
                }

            }
        }

        protected String m_Name;
        public String Name
        {
            get
            {
                return m_Name;
            }
            set
            {
                m_Name = value;
            }
        }
        //总宽度
        protected Single m_TotalWidth;
        public Single TotalWidth
        {
            get
            {
                return m_TotalWidth;
            }
            set
            {
                m_TotalWidth = value;
            }
        }
        //冠宽度
        public Single TotalJianWidth
        {
            get
            {
                if (this.NominalType != NominalType.SingleSymm || this.NominalType != NominalType.SingleWidthSymm) return 0;
                for (int i = 0; i < m_MyPointDatas.Count; i++)
                {
                    if (m_MyPointDatas[i].IsTaiJian)
                    {
                        return Math.Abs(TotalWidth - m_MyPointDatas[i].Point.X * 2);
                    }
                }
                return 0;
            }

        }
        //宽度公差
        protected Single m_WidthTol;
        public Single WidthTol
        {
            get
            {
                return m_WidthTol;
            }
            set
            {
                m_WidthTol = value;
            }
        }
        //标准厚度
        protected Single m_RimHeight;
        public Single RimHeight
        {
            get
            {
                return m_RimHeight;
            }
            set
            {
                m_RimHeight = value;
            }
        }
        //厚度公差
        protected Single m_RimHeightTol;
        public Single RimHeightTol
        {
            get
            {
                return m_RimHeightTol;
            }
            set
            {
                m_RimHeightTol = value;
            }
        }
        //'面积公差
        protected Single m_AreaDiff;
        public Single AreaDiff
        {
            get
            {
                return m_AreaDiff;
            }
            set
            {
                m_AreaDiff = value;
            }
        }

        //'数据库中的"TextNo"字段
        protected string m_DieNo;
        public string DieNo
        {
            get
            {
                return m_DieNo;
            }
            set
            {
                m_DieNo = value;
            }
        }
        //m_ShoulderWidth 
        protected Single m_ShoulderWidth;
        public Single ShoulderWidth
        {
            get
            {
                return m_ShoulderWidth;
            }
        }

        protected Single m_ShoulderWidthTol;
        public Single ShoulderWidthTol
        {
            get
            {
                return m_ShoulderWidthTol;
            }
        }

        protected Single m_ShoulderHeight;
        public Single ShoulderHeight
        {
            get
            {
                return m_ShoulderHeight;
            }
        }

        protected Single m_ShoulderHeightTol;
        public Single ShoulderHeightTol
        {
            get
            {
                return m_ShoulderHeightTol;
            }
        }
        //'数据库中的"jiao"字段
        protected string m_jiao;
        public string Jiao
        {
            get
            {
                return m_jiao;
            }
        }

        protected string m_DateTimePicker;
        public string DateTimePicker
        {
            get
            {
                return m_DateTimePicker;
            }
        }
        //'数据检索ID
        private int m_DBID;
        public int DBID
        {
            get
            {
                return m_DBID;
            }
        }
        private int m_DBID1;
        public int DBID1
        {
            get
            {
                return m_DBID1;
            }
        }
        //OK结果判定百分比
        private int m_OKPercent;
        public int OKPercent
        {
            get
            {
                return m_OKPercent;
            }
            set
            {
                m_OKPercent = value;
            }
        }
        private CenterType m_FindMidType;
        public CenterType FindMidType
        {
            get { return m_FindMidType; }
            set { m_FindMidType = value; }
        }
        #endregion
        //'构造函数
        public ClsMyNominal()
        {
            m_MyPointDatas = new NominalPointDataList();
        }
        public ClsMyNominal(PointDataList pdataList, NominalType Type)
        {
            this.m_NominalType = Type;
            m_ThicknessPoints = new PointDataList();
            m_ThicknessPoints = pdataList;
            m_MyPointDatas = new NominalPointDataList();
            for (int i = 0; i < pdataList.Count; i++)
            {
                m_MyPointDatas.Add(new NominalPointData(pdataList[i].Point, true));
            }
            m_Size = m_ThicknessPoints.Size;
            switch (this.m_NominalType)
            {
                case NominalType.SingleSymm:
                    m_IsSymm = true;
                    break;
                case NominalType.DoubleSymm:
                    m_IsSymm = true;
                    break;
                case NominalType.SingleNoSymm:
                    m_IsSymm = false;
                    break;
                case NominalType.SingleWidthSymm:
                    m_IsSymm = true;
                    break;
                default:
                    m_IsSymm = true;
                    break;
            }
            if (m_ThicknessPoints.Count > 0)
            {
                if (m_IsSymm)
                {
                    m_XReference = m_ThicknessPoints[0].Point.X + m_Size.Width / 2;
                }
                else
                {
                    m_XReference = m_ThicknessPoints[0].Point.X;
                }
            }
        }
        //'构造函数
        public ClsMyNominal(NominalType Type)
        {
            this.m_NominalType = Type;
            m_ThicknessPoints = new PointDataList();
            m_MyPointDatas = new NominalPointDataList();
            m_Size = new SizeF(0, 0);
            switch (this.m_NominalType)
            {
                case NominalType.SingleSymm:
                    m_IsSymm = true;
                    break;
                case NominalType.DoubleSymm:
                    m_IsSymm = true;
                    break;
                case NominalType.SingleNoSymm:
                    m_IsSymm = false;
                    break;
                case NominalType.SingleWidthSymm:
                    m_IsSymm = true;
                    break;
                default:
                    m_IsSymm = true;
                    break;
            }
            m_XReference = 0;
        }
        //构造函数
        public ClsMyNominal(int ID)
        {
            SelectDB(ID); //填充m_ThicknessPoints
            m_Size = m_ThicknessPoints.Size;
            if (m_IsSymm)
            {
                m_XReference = m_ThicknessPoints[0].Point.X + m_Size.Width / 2;
            }
            else
            {
                m_XReference = m_ThicknessPoints[0].Point.X;
            }
        }
        public ClsMyNominal(string ID)
        {
            SelectDB(ID); //填充m_ThicknessPoints
            m_Size = m_ThicknessPoints.Size;
            if (m_IsSymm)
            {
                m_XReference = m_ThicknessPoints[0].Point.X + m_Size.Width / 2;
            }
            else
            {
                m_XReference = m_ThicknessPoints[0].Point.X;
            }
        }
        public ClsMyNominal(DataRow r)
        {

            this.m_Name = r["ProfileName"].ToString();
            if (r["NominalType"].ToString() == "")
            {
                //MessageBox.Show("规格中的对称类型不能设置为空!");
            }
            switch (Convert.ToInt32(r["NominalType"]))
            {
                case 0:
                    this.m_NominalType = NominalType.SingleSymm;
                    m_IsSymm = true;
                    break;
                case 1:
                    this.m_NominalType = NominalType.DoubleSymm;
                    m_IsSymm = true;
                    break;
                case 3:
                    this.m_NominalType = NominalType.SingleWidthSymm;
                    m_IsSymm = true;
                    break;
                default:
                    m_IsSymm = false;
                    this.m_NominalType = NominalType.SingleNoSymm;
                    break;

            }
            if (r["FindMidType"].ToString() == "")
            {
                //MessageBox.Show("规格中的参考线查找类型不能设置为空!");
            }
            switch (Convert.ToInt32(r["FindMidType"]))
            {
                case 0:
                    this.m_FindMidType = CenterType.None;
                    break;
                case 1:
                    this.m_FindMidType = CenterType.Lowest;
                    break;
                default:
                    this.m_FindMidType = CenterType.Highest;
                    break;

            }

            this.m_RimHeight = Convert.ToSingle(r["RimHeight"]);
            this.m_RimHeightTol = Convert.ToSingle(r["RimTol"]);
            this.m_TotalWidth = Convert.ToSingle(r["TotalWidth"]);
            this.m_WidthTol = Convert.ToSingle(r["WidthTol"]);
            //*********************************************************
            m_DieNo = r["TextNo"].ToString();
            m_ShoulderWidth = Convert.ToSingle(r["ShoulderWidth"]);
            m_ShoulderWidthTol = Convert.ToSingle(r["ShoulderWidthTol"]);
            m_ShoulderHeight = Convert.ToSingle(r["ShoulderHeight"]);
            m_ShoulderHeightTol = Convert.ToSingle(r["ShoulderHeightTol"]);
            m_jiao = r["jiao"].ToString();
            m_DateTimePicker = r["DateTimePicker"].ToString();
            DateTime dt2 = DateTime.Now;
            //try
            //{
            //    clsAccess db1 = new clsAccess();
            //    db1.Open(Application.StartupPath + "\\WeightDataBase.mdb");
            //    String str = "select * from SpecInfo where ProfileName='" + this.Name + "'";
            //    DataTable dt1 = db1.GetDataTable(str);

            //    if (dt1.Rows.Count > 0)
            //    {
            //        this.WeightSD = Convert.ToSingle(dt1.Rows[0]["WeightSD"]);
            //        this.WeightTolUp = Convert.ToSingle(dt1.Rows[0]["WeightTolUp"]);
            //        this.WeightTolDown = Convert.ToSingle(dt1.Rows[0]["WeightTolDown"]);
            //        this.WeightAlmUp = Convert.ToSingle(dt1.Rows[0]["WeightAlmUp"]);
            //        this.WeightAlmDown = Convert.ToSingle(dt1.Rows[0]["WeightAlmDown"]);
            //        m_DBID1 = Convert.ToInt32(dt1.Rows[0]["id"]);
            //    }
            //    if (dt1.Rows.Count == 0)
            //    {
            //        this.WeightSD = 0;
            //        this.WeightTolUp = 0;
            //        this.WeightTolDown = 0;
            //        this.WeightAlmUp = 0;
            //        this.WeightAlmDown = 0;
            //    }
            //    db1.Close();
            //}
            //catch(Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //}    //***********************************************************
            //DateTime dt3 = DateTime.Now;
            //MessageBox.Show((dt3 - dt2).TotalSeconds.ToString());
            this.m_AreaDiff = Convert.ToSingle(r["AreaTol"]);
            //'数据存储格式“-150#0.5#3#0.5#-1*-100#10#3#0.5#-1*100#10#3#0.5#-1*150#0.5#3#0.5#-1*”
            //'将字符串数据转换为m_ThicknessPoints
            m_MyPointDatas = NominalDataHanding.DBStr(r["GJD"].ToString());
            this.m_OKPercent = int.Parse(r["ResultPercent"].ToString());
            //this.m_FindMidType = (Profile.CenterType)Enum.Parse(typeof(Profile.CenterType), r["FindMidType"].ToString(), true);
            m_ThicknessPoints.Clear();
            for (int i = 0; i < m_MyPointDatas.Count; i++)
            {
                m_ThicknessPoints.Add((PointData)m_MyPointDatas[i]);
            }

            m_DBID = Convert.ToInt32(r["id"]);
            m_Size = m_ThicknessPoints.Size;
            if (m_IsSymm)
            {
                m_XReference = m_ThicknessPoints[0].Point.X + m_Size.Width / 2;
            }
            else if (this.m_NominalType == NominalType.SingleNoSymm)
            {
                m_XReference = m_ThicknessPoints[0].Point.X + m_Size.Width / 2;
            }
            else
            {
                m_XReference = m_ThicknessPoints[0].Point.X;
            }


        }
        public void SetInitInfo(NominalType Type, String Name, Single TotalWidth, Single WidthTol,
            Single RimHeight, Single RimTol, Single AreaDiff, string DieNo, Single ShoulderWidth,
            Single ShoulderWidthTol, Single ShoulderHeight, Single ShoulderHeightTol, string DateTimePicker,
            string jiao, int Percent, CenterType CenterType)
        {
            this.m_Name = Name;
            this.m_TotalWidth = TotalWidth;
            this.m_WidthTol = WidthTol;
            this.m_RimHeight = RimHeight;
            this.m_RimHeightTol = RimTol;
            m_AreaDiff = AreaDiff;
            this.m_NominalType = Type;
            //********************************
            m_DieNo = DieNo;
            m_ShoulderWidth = ShoulderWidth;
            m_ShoulderWidthTol = ShoulderWidthTol;
            m_ShoulderHeight = ShoulderHeight;
            m_ShoulderHeightTol = ShoulderHeightTol;
            m_jiao = jiao;
            m_DateTimePicker = DateTimePicker;
            this.m_OKPercent = Percent;
            this.m_FindMidType = CenterType;
            //********************************

            //this.m_ThicknessPoints = new ProfilePFColl();
            NominalPointData ppf1, ppf2, ppf3;
            Boolean b1 = false, b2 = false, b3 = false;
            //自动添加边缘关键点
            switch (this.NominalType)
            {
                case NominalType.DoubleSymm://双橡胶【对称】规格
                    m_IsSymm = true;
                    ppf1 = new NominalPointData(new PointF(0, RimHeight), WidthTol, RimTol, true);
                    ppf2 = new NominalPointData(new PointF((TotalWidth), RimHeight), WidthTol, RimTol, true);
                    ppf3 = new NominalPointData(new PointF((TotalWidth) * 2, RimHeight), WidthTol, RimTol, true);

                    for (int i = 0; i < m_MyPointDatas.Count; i++)
                    {
                        if (m_MyPointDatas[i].Point.X == ppf1.Point.X)
                        {
                            b1 = true;
                        }
                    }
                    for (int i = 0; i < m_MyPointDatas.Count; i++)
                    {
                        if (m_MyPointDatas[i].Point.X == ppf2.Point.X)
                        {
                            b2 = true;
                        }
                    }
                    for (int i = 0; i < m_MyPointDatas.Count; i++)
                    {
                        if (m_MyPointDatas[i].Point.X == ppf3.Point.X)
                        {
                            b3 = true;
                        }
                    }
                    if (b1 == false)
                        m_MyPointDatas.Add(ppf1);
                    if (b2 == false)
                        m_MyPointDatas.Add(ppf2);
                    if (b3 == false)
                        m_MyPointDatas.Add(ppf3);
                    break;
                case NominalType.SingleSymm://"单橡胶【对称】规格"
                    m_IsSymm = true;
                    ppf1 = new NominalPointData(new PointF(0F, RimHeight), WidthTol, RimTol, true);
                    ppf2 = new NominalPointData(new PointF(TotalWidth, RimHeight), WidthTol, RimTol, true);
                    for (int i = 0; i < m_MyPointDatas.Count; i++)
                    {
                        if (m_MyPointDatas[i].Point.X == ppf1.Point.X)
                        {
                            b1 = true;
                        }
                    }
                    for (int i = 0; i < m_MyPointDatas.Count; i++)
                    {
                        if (m_MyPointDatas[i].Point.X == ppf2.Point.X)
                        {
                            b2 = true;
                        }
                    }
                    if (b1 == false)
                        m_MyPointDatas.Add(ppf1);
                    if (b2 == false)
                        m_MyPointDatas.Add(ppf2);
                    break;
                case NominalType.SingleNoSymm://单橡胶【不对称】规格
                    m_IsSymm = false;
                    ppf1 = new NominalPointData(new PointF(0, RimHeight), WidthTol, RimTol, true);
                    ppf2 = new NominalPointData(new PointF(TotalWidth, RimHeight), WidthTol, RimTol, true);
                    for (int i = 0; i < m_MyPointDatas.Count; i++)
                    {
                        if (m_MyPointDatas[i].Point.X == ppf1.Point.X)
                        {
                            b1 = true;
                        }
                    }
                    for (int i = 0; i < m_MyPointDatas.Count; i++)
                    {
                        if (m_MyPointDatas[i].Point.X == ppf2.Point.X)
                        {
                            b2 = true;
                        }
                    }
                    if (b1 == false)
                        m_MyPointDatas.Add(ppf1);
                    if (b2 == false)
                        m_MyPointDatas.Add(ppf2);
                    break;
                case NominalType.SingleWidthSymm://单橡胶【宽度对称】规格
                    m_IsSymm = false;
                    ppf1 = new NominalPointData(new PointF(0, RimHeight), WidthTol, RimTol, true);
                    ppf2 = new NominalPointData(new PointF(TotalWidth, RimHeight), WidthTol, RimTol, true);
                    for (int i = 0; i < m_MyPointDatas.Count; i++)
                    {
                        if (m_MyPointDatas[i].Point.X == ppf1.Point.X)
                        {
                            b1 = true;
                        }
                    }
                    for (int i = 0; i < m_MyPointDatas.Count; i++)
                    {
                        if (m_MyPointDatas[i].Point.X == ppf2.Point.X)
                        {
                            b2 = true;
                        }
                    }
                    if (b1 == false)
                        m_MyPointDatas.Add(ppf1);
                    if (b2 == false)
                        m_MyPointDatas.Add(ppf2);
                    break;
            }
            m_MyPointDatas.AscSortForX();
            m_ThicknessPoints.Clear();
            for (int i = 0; i < m_MyPointDatas.Count; i++)
            {
                m_ThicknessPoints.Add((PointData)m_MyPointDatas[i]);
            }
            m_ThicknessPoints.AscSortForX();
            m_Size = m_ThicknessPoints.Size;
            if (m_ThicknessPoints.Count > 0)
            {
                if (m_IsSymm)
                {
                    m_XReference = m_ThicknessPoints[0].Point.X + m_Size.Width / 2;
                }
                else
                {

                    m_XReference = m_ThicknessPoints[0].Point.X;
                }
            }
            if (this.DataChanged != null) DataChanged();
        }
        public void SetInitInfo(string Name, Single TotalWidth, Single WidthTol, Single RimHeight, Single RimTol,
            Single AreaTol, string StrPointList, string DieNo, Single ShoulderWidth, Single ShoulderWidthTol,
            Single ShoulderHeight, Single ShoulderHeightTol, string DateTimePicker, string jiao, int Percent,
            CenterType CenterType)
        {
            this.m_Name = Name;
            this.m_TotalWidth = TotalWidth;
            this.m_WidthTol = WidthTol;
            this.m_RimHeight = RimHeight;
            this.m_RimHeightTol = RimTol;
            m_AreaDiff = AreaTol;
            this.m_OKPercent = Percent;
            //********************************
            m_DieNo = DieNo;
            m_ShoulderWidth = ShoulderWidth;
            m_ShoulderWidthTol = ShoulderWidthTol;
            m_ShoulderHeight = ShoulderHeight;
            m_ShoulderHeightTol = ShoulderHeightTol;
            m_jiao = jiao;
            m_DateTimePicker = DateTimePicker;
            this.m_FindMidType = CenterType;
            //********************************
            //m_ThicknessPoints.DBStr(StrPointList);
            m_MyPointDatas = NominalDataHanding.DBStr(StrPointList);
            m_MyPointDatas.AscSortForX();
            m_ThicknessPoints.Clear();
            for (int i = 0; i < m_MyPointDatas.Count; i++)
            {
                m_ThicknessPoints.Add((PointData)m_MyPointDatas[i]);
            }

            m_Size = m_ThicknessPoints.Size;
            if (m_ThicknessPoints.Count > 0)
            {
                if (m_IsSymm)
                {
                    m_XReference = m_ThicknessPoints[0].Point.X + m_Size.Width / 2;
                }
                else
                {
                    m_XReference = m_ThicknessPoints[0].Point.X + m_Size.Width / 2;
                }
            }

            if (this.DataChanged != null) DataChanged();
        }
        //添加关键点--左参考线
        public void AddNominalPF(NominalPointData HalfProfilePF, Boolean IfMid)
        {
            NominalPointData s1, s2;
            switch (this.NominalType)
            {
                case NominalType.SingleNoSymm://单橡胶不对称

                    this.m_MyPointDatas.Add(HalfProfilePF);
                    break;
                case NominalType.SingleSymm://单橡胶对称
                    if (IfMid == true)
                    {
                        s1 = new NominalPointData(new PointF(m_TotalWidth / 2 - (HalfProfilePF.Point.X), HalfProfilePF.Point.Y), HalfProfilePF.XOffset, HalfProfilePF.YOffset, HalfProfilePF.IsCheck, HalfProfilePF.IsCheckDuiChen, HalfProfilePF.TolDuiChenThick);
                        this.m_MyPointDatas.Add(s1);
                        break;//HalfProfilePF
                    }
                    //s1 = HalfProfilePF;
                    //s2 = HalfProfilePF;
                    //s.Point.X = -(HalfProfilePF.X);
                    s1 = new NominalPointData(new PointF(m_TotalWidth / 2 - (HalfProfilePF.Point.X), HalfProfilePF.Point.Y), HalfProfilePF.XOffset, HalfProfilePF.YOffset, HalfProfilePF.IsCheck, HalfProfilePF.IsCheckDuiChen, HalfProfilePF.TolDuiChenThick);
                    s1.IsTaiJian = HalfProfilePF.IsTaiJian;
                    s2 = new NominalPointData(new PointF(m_TotalWidth - s1.Point.X, HalfProfilePF.Point.Y), HalfProfilePF.XOffset, HalfProfilePF.YOffset, HalfProfilePF.IsCheck, HalfProfilePF.IsCheckDuiChen, HalfProfilePF.TolDuiChenThick);
                    s2.IsTaiJian = HalfProfilePF.IsTaiJian;
                    this.m_MyPointDatas.Add(s1);
                    this.m_MyPointDatas.Add(s2);
                    break;
                case NominalType.DoubleSymm:
                    //s1 = HalfProfilePF;
                    //s2 = HalfProfilePF;
                    //s.Point.X = -(HalfProfilePF.X);
                    s1 = new NominalPointData(new PointF(m_TotalWidth - (HalfProfilePF.Point.X), HalfProfilePF.Point.Y), HalfProfilePF.XOffset, HalfProfilePF.YOffset, HalfProfilePF.IsCheck, HalfProfilePF.IsCheckDuiChen, HalfProfilePF.TolDuiChenThick);
                    s2 = new NominalPointData(new PointF(m_TotalWidth + HalfProfilePF.Point.X, HalfProfilePF.Point.Y), HalfProfilePF.XOffset, HalfProfilePF.YOffset, HalfProfilePF.IsCheck, HalfProfilePF.IsCheckDuiChen, HalfProfilePF.TolDuiChenThick);
                    this.m_MyPointDatas.Add(s1);
                    this.m_MyPointDatas.Add(s2);
                    break;
                case NominalType.SingleWidthSymm://单橡胶不对称

                    this.m_MyPointDatas.Add(HalfProfilePF);
                    break;
                default://对称,以左侧为参考线
                    //s1 = HalfProfilePF;
                    //s2 = HalfProfilePF;
                    //s.Point.X = -(HalfProfilePF.X);
                    s1 = new NominalPointData(new PointF(m_TotalWidth / 2 - (HalfProfilePF.Point.X), HalfProfilePF.Point.Y), HalfProfilePF.XOffset, HalfProfilePF.YOffset, HalfProfilePF.IsCheck, HalfProfilePF.IsCheckDuiChen, HalfProfilePF.TolDuiChenThick);
                    s2 = new NominalPointData(new PointF(m_TotalWidth - s1.Point.X, HalfProfilePF.Point.Y), HalfProfilePF.XOffset, HalfProfilePF.YOffset, HalfProfilePF.IsCheck, HalfProfilePF.IsCheckDuiChen, HalfProfilePF.TolDuiChenThick);
                    this.m_MyPointDatas.Add(s1);
                    this.m_MyPointDatas.Add(s2);
                    break;
            }
            m_MyPointDatas.AscSortForX();
            m_ThicknessPoints.Clear();
            for (int i = 0; i < m_MyPointDatas.Count; i++)
            {
                m_ThicknessPoints.Add((PointData)m_MyPointDatas[i]);
            }
            if (this.DataChanged != null) DataChanged();
        }
        //删除关键点
        public void RemoveNominalPF(NominalPointData HalfProfilePF)
        {
            NominalPointData s1, s2;
            switch (this.NominalType)
            {
                case NominalType.SingleNoSymm:
                    //m_ThicknessPoints.Remove(HalfProfilePF);
                    PDListRemove(ref m_MyPointDatas, HalfProfilePF.Point.X);
                    break;
                case NominalType.SingleSymm:
                    s1 = new NominalPointData(new PointF(HalfProfilePF.Point.X, HalfProfilePF.Point.Y), HalfProfilePF.XOffset, HalfProfilePF.YOffset, HalfProfilePF.IsCheck);//HalfProfilePF;
                    s2 = new NominalPointData(new PointF(m_TotalWidth / 2 + (m_TotalWidth / 2 - s1.Point.X), HalfProfilePF.Point.Y), HalfProfilePF.XOffset, HalfProfilePF.YOffset, HalfProfilePF.IsCheck); //HalfProfilePF;
                    //m_ThicknessPoints.Remove(s1);
                    //m_ThicknessPoints.Remove(s2);
                    PDListRemove(ref m_MyPointDatas, s1.Point.X);
                    PDListRemove(ref m_MyPointDatas, s2.Point.X);
                    break;
                case NominalType.DoubleSymm:
                    s1 = new NominalPointData(new PointF(HalfProfilePF.Point.X, HalfProfilePF.Point.Y), HalfProfilePF.XOffset, HalfProfilePF.YOffset, HalfProfilePF.IsCheck);//HalfProfilePF;
                    s2 = new NominalPointData(new PointF(m_TotalWidth + (m_TotalWidth - s1.Point.X), HalfProfilePF.Point.Y), HalfProfilePF.XOffset, HalfProfilePF.YOffset, HalfProfilePF.IsCheck);
                    //m_ThicknessPoints.Remove(s1);
                    //m_ThicknessPoints.Remove(s2);
                    PDListRemove(ref m_MyPointDatas, s1.Point.X);
                    PDListRemove(ref m_MyPointDatas, s2.Point.X);
                    break;
                case NominalType.SingleWidthSymm:
                    //m_ThicknessPoints.Remove(HalfProfilePF);
                    PDListRemove(ref m_MyPointDatas, HalfProfilePF.Point.X);
                    break;
                default:
                    s1 = new NominalPointData(new PointF(HalfProfilePF.Point.X, HalfProfilePF.Point.Y), HalfProfilePF.XOffset, HalfProfilePF.YOffset, HalfProfilePF.IsCheck);//HalfProfilePF;
                    s2 = new NominalPointData(new PointF(m_TotalWidth / 2 + (m_TotalWidth / 2 - s1.Point.X), HalfProfilePF.Point.Y), HalfProfilePF.XOffset, HalfProfilePF.YOffset, HalfProfilePF.IsCheck); //HalfProfilePF;
                    //m_ThicknessPoints.Remove(s1);
                    //m_ThicknessPoints.Remove(s2);
                    PDListRemove(ref m_MyPointDatas, s1.Point.X);
                    PDListRemove(ref m_MyPointDatas, s2.Point.X);
                    break;
            }
            m_MyPointDatas.AscSortForX();
            m_ThicknessPoints.Clear();
            for (int i = 0; i < m_MyPointDatas.Count; i++)
            {
                m_ThicknessPoints.Add((PointData)m_MyPointDatas[i]);
            }
            if (this.DataChanged != null) DataChanged();
        }
        private void PDListRemove(ref NominalPointDataList ThicknessPoints, Single value)
        {
            //'List.Remove(value)
            for (int i = 0; i < this.MyKeyPoints.Count; i++)//- 1
            {
                if (value == MyKeyPoints[i].Point.X)
                {
                    MyKeyPoints.RemoveAt(i);
                    break;
                }
            }
        } //'Remove
        public int NominalLinePointCount()
        {
            return this.m_ThicknessPoints.Count;
        }
        public NominalPointData NominalLinePoint(int Index)
        {
            return this.m_MyPointDatas[Index];
        }

        public string TypeToStr(NominalType Type)
        {
            switch (Type)
            {
                case NominalType.DoubleSymm:
                    return "双橡胶对称胶材质";
                case NominalType.SingleSymm:
                    return "单橡胶对称胶材质";
                default:
                    return "单橡胶不对称材质";
            }
        }
        //将关键点序列转换为字符串
        public string NominalPDColToStr()
        {
            return NominalDataHanding.DBStr(this.MyKeyPoints);
        }
        #region 数据库操作--SpecInfo表
        //从数据库SpecInfo表中,查找相应Id的记录，将对应的关键点字符串转换为m_ThicknessPoints
        public void SelectDB(int Id)
        {
            try
            {
                String str = "select * from SpecInfo where id=" + Id;
                this.m_ThicknessPoints = new PointDataList();
                this.m_MyPointDatas = new NominalPointDataList();
                using (clsComDB db = clsDBManager.CreateComDB())
                {
                    db.Open();
                    DataTable dt = db.GetDataTable(str);
                    DataRow r = dt.Rows[0];
                    this.m_Name = r["ProfileName"].ToString();
                    if (r["NominalType"].ToString() == "")
                    {
                        //MessageBox.Show("规格中的对称类型不能设置为空!");
                    }
                    switch (Convert.ToInt32(r["NominalType"]))
                    {
                        case 0:
                            this.m_NominalType = NominalType.SingleSymm;
                            m_IsSymm = true;
                            break;
                        case 1:
                            this.m_NominalType = NominalType.DoubleSymm;
                            m_IsSymm = true;
                            break;
                        case 3:
                            this.m_NominalType = NominalType.SingleWidthSymm;
                            m_IsSymm = true;
                            break;
                        default:
                            m_IsSymm = false;
                            this.m_NominalType = NominalType.SingleNoSymm;
                            break;

                    }
                    if (r["FindMidType"].ToString() == "")
                    {
                        //MessageBox.Show("规格中的参考线查找类型不能设置为空!");
                    }
                    switch (Convert.ToInt32(r["FindMidType"]))
                    {
                        case 0:
                            this.m_FindMidType = CenterType.None;
                            break;
                        case 1:
                            this.m_FindMidType = CenterType.Lowest;
                            break;
                        default:
                            this.m_FindMidType = CenterType.Highest;
                            break;

                    }

                    this.m_RimHeight = Convert.ToSingle(r["RimHeight"]);
                    this.m_RimHeightTol = Convert.ToSingle(r["RimTol"]);
                    this.m_TotalWidth = Convert.ToSingle(r["TotalWidth"]);
                    this.m_WidthTol = Convert.ToSingle(r["WidthTol"]);
                    //*********************************************************
                    m_DieNo = r["TextNo"].ToString();
                    m_ShoulderWidth = Convert.ToSingle(r["ShoulderWidth"]);
                    m_ShoulderWidthTol = Convert.ToSingle(r["ShoulderWidthTol"]);
                    m_ShoulderHeight = Convert.ToSingle(r["ShoulderHeight"]);
                    m_ShoulderHeightTol = Convert.ToSingle(r["ShoulderHeightTol"]);
                    m_jiao = r["jiao"].ToString();
                    m_DateTimePicker = r["DateTimePicker"].ToString();
                    try
                    {
                        this.WeightSD = Convert.ToSingle(r["WeightSD"]);
                        this.WeightTolUp = Convert.ToSingle(r["WeightTolUp"]);
                        this.WeightTolDown = Convert.ToSingle(r["WeightTolDown"]);
                        this.WeightAlmUp = Convert.ToSingle(r["WeightAlmUp"]);
                        this.WeightAlmDown = Convert.ToSingle(r["WeightAlmDown"]);
                    }
                    catch
                    {

                    }
                    //***********************************************************
                    this.m_AreaDiff = Convert.ToSingle(r["AreaTol"]);
                    //'数据存储格式“-150#0.5#3#0.5#-1*-100#10#3#0.5#-1*100#10#3#0.5#-1*150#0.5#3#0.5#-1*”
                    //'将字符串数据转换为m_ThicknessPoints
                    m_MyPointDatas = NominalDataHanding.DBStr(r["GJD"].ToString());
                    this.m_OKPercent = int.Parse(r["ResultPercent"].ToString());
                    //this.m_FindMidType = (Profile.CenterType)Enum.Parse(typeof(Profile.CenterType), r["FindMidType"].ToString(), true);
                    m_ThicknessPoints.Clear();
                    for (int i = 0; i < m_MyPointDatas.Count; i++)
                    {
                        m_ThicknessPoints.Add((PointData)m_MyPointDatas[i]);
                    }
                }
                m_DBID = Id;
            }
            catch (Exception ex)
            {
                //throw new ArgumentNullException();
                //MessageBox.Show(ex.Message.ToString());
                return;
            }

        }
        public void SelectDB(string TypeName)
        {
            try
            {
                String str = "select * from SpecInfo where ProfileName='" + TypeName + "'";
                this.m_ThicknessPoints = new PointDataList();
                this.m_MyPointDatas = new NominalPointDataList();
                using (clsComDB db = clsDBManager.CreateComDB())
                {
                    db.Open();
                    DataTable dt = db.GetDataTable(str);
                    DataRow r = dt.Rows[0];
                    this.m_Name = r["ProfileName"].ToString();
                    if (r["NominalType"].ToString() == "")
                    {
                        //MessageBox.Show("规格中的对称类型不能设置为空!");
                    }
                    switch (Convert.ToInt32(r["NominalType"]))
                    {
                        case 0:
                            this.m_NominalType = NominalType.SingleSymm;
                            m_IsSymm = true;
                            break;
                        case 1:
                            this.m_NominalType = NominalType.DoubleSymm;
                            m_IsSymm = true;
                            break;
                        case 3:
                            this.m_NominalType = NominalType.SingleWidthSymm;
                            m_IsSymm = true;
                            break;
                        default:
                            m_IsSymm = false;
                            this.m_NominalType = NominalType.SingleNoSymm;
                            break;

                    }
                    if (r["FindMidType"].ToString() == "")
                    {
                        //MessageBox.Show("规格中的参考线查找类型不能设置为空!");
                    }
                    switch (Convert.ToInt32(r["FindMidType"]))
                    {
                        case 0:
                            this.m_FindMidType = CenterType.None;
                            break;
                        case 1:
                            this.m_FindMidType = CenterType.Lowest;
                            break;
                        default:
                            this.m_FindMidType = CenterType.Highest;
                            break;

                    }

                    this.m_RimHeight = Convert.ToSingle(r["RimHeight"]);
                    this.m_RimHeightTol = Convert.ToSingle(r["RimTol"]);
                    this.m_TotalWidth = Convert.ToSingle(r["TotalWidth"]);
                    this.m_WidthTol = Convert.ToSingle(r["WidthTol"]);
                    //*********************************************************
                    m_DieNo = r["TextNo"].ToString();
                    m_ShoulderWidth = Convert.ToSingle(r["ShoulderWidth"]);
                    m_ShoulderWidthTol = Convert.ToSingle(r["ShoulderWidthTol"]);
                    m_ShoulderHeight = Convert.ToSingle(r["ShoulderHeight"]);
                    m_ShoulderHeightTol = Convert.ToSingle(r["ShoulderHeightTol"]);
                    m_jiao = r["jiao"].ToString();
                    try
                    {
                        this.WeightSD = Convert.ToSingle(r["WeightSD"]);
                        this.WeightTolUp = Convert.ToSingle(r["WeightTolUp"]);
                        this.WeightTolDown = Convert.ToSingle(r["WeightTolDown"]);
                        this.WeightAlmUp = Convert.ToSingle(r["WeightAlmUp"]);
                        this.WeightAlmDown = Convert.ToSingle(r["WeightAlmDown"]);
                    }
                    catch
                    {

                    }
                    m_DateTimePicker = r["DateTimePicker"].ToString();
                    //***********************************************************
                    this.m_AreaDiff = Convert.ToSingle(r["AreaTol"]);
                    //'数据存储格式“-150#0.5#3#0.5#-1*-100#10#3#0.5#-1*100#10#3#0.5#-1*150#0.5#3#0.5#-1*”
                    //'将字符串数据转换为m_ThicknessPoints
                    m_MyPointDatas = NominalDataHanding.DBStr(r["GJD"].ToString());
                    this.m_OKPercent = int.Parse(r["ResultPercent"].ToString());
                    //this.m_FindMidType = (Profile.CenterType)Enum.Parse(typeof(Profile.CenterType), r["FindMidType"].ToString(), true);
                    m_ThicknessPoints.Clear();
                    for (int i = 0; i < m_MyPointDatas.Count; i++)
                    {
                        m_ThicknessPoints.Add((PointData)m_MyPointDatas[i]);
                    }
                    m_DBID = Convert.ToInt32(r["ID"]);
                }
            }
            catch (Exception ex)
            {
                //throw new ArgumentNullException();
                //MessageBox.Show(ex.Message.ToString());
                return;
            }

        }
        //'向数据库SpecInfo表中，插入记录
        public Boolean InsertDB()
        {
            int m_Type = 0;
            switch (this.NominalType)
            {
                case NominalType.SingleSymm:
                    m_Type = 0;
                    break;
                case NominalType.DoubleSymm:
                    m_Type = 1;
                    break;
                case NominalType.SingleNoSymm:
                    m_Type = 2;
                    break;
                case NominalType.SingleWidthSymm:
                    m_Type = 3;
                    break;
            }

            int m_MidType = 0;
            switch (this.FindMidType)
            {
                case CenterType.None:
                    m_MidType = 0;
                    break;
                case CenterType.Lowest:
                    m_MidType = 1;
                    break;
                case CenterType.Highest:
                    m_MidType = 2;
                    break;
            }

            String str = "select * from SpecInfo where ProfileName='" + this.Name + "'";
            using (clsComDB db = clsDBManager.CreateComDB())
            {
                db.Open();
                DataTable dt = db.GetDataTable(str);
                //DataRow r = dt.Rows[0];
                if (dt.Rows.Count > 0)
                {
                    //if (MessageBox.Show("已经有名称为" + this.Name + "的轮廓规格了!要更新吗!", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    //{
                    //    m_DBID = Convert.ToInt32(dt.Rows[0]["ID"].ToString());
                    //    UpdateDB(DBID);
                    //    return true;
                    //}
                    return false;
                }
                db.Close();
            }
            str = "Insert Into SpecInfo (" +
                               "ProfileName,NominalType," +
                               "RimHeight,RimTol," +
                               "TotalWidth,WidthTol," +
                               "TextNo,ShoulderWidth," +
                               "ShoulderWidthTol,ShoulderHeight," +
                               "ShoulderHeightTol," +
                               "jiao," +
                               "DateTimePicker," +
                               "AreaTol, GJD,ResultPercent,FindMidType) " +
                               "Values (" +
                               "'" + this.Name + "'," + m_Type + "," +
                               "" + this.RimHeight + "," + this.m_RimHeightTol.ToString("f2") + "," +
                               "" + this.TotalWidth + "," + this.WidthTol + "," +
                               "'" + this.DieNo + "'," + this.ShoulderWidth + "," +
                               "" + this.ShoulderWidthTol + "," + this.ShoulderHeight + "," +
                               "" + this.ShoulderHeightTol + "," +
                               "'" + this.Jiao + "'," +
                               "'" + this.DateTimePicker + "'," +
                               "" + this.AreaDiff + "," + "'" + NominalDataHanding.DBStr(this.MyKeyPoints) + "'," + this.OKPercent + "," + m_MidType + ")";
            try
            {
                using (clsComDB db = clsDBManager.CreateComDB())
                {
                    db.Open();
                    db.ExecSql(str);
                    return true;
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message.ToString());
                return false;
            }
        }

        public Boolean InsertDBWeight()
        {
            int m_Type = 0;
            switch (this.NominalType)
            {
                case NominalType.SingleSymm:
                    m_Type = 0;
                    break;
                case NominalType.DoubleSymm:
                    m_Type = 1;
                    break;
                case NominalType.SingleNoSymm:
                    m_Type = 2;
                    break;
                case NominalType.SingleWidthSymm:
                    m_Type = 3;
                    break;
            }

            int m_MidType = 0;
            switch (this.FindMidType)
            {
                case CenterType.None:
                    m_MidType = 0;
                    break;
                case CenterType.Lowest:
                    m_MidType = 1;
                    break;
                case CenterType.Highest:
                    m_MidType = 2;
                    break;
            }


            return true;
        }
        //'数据库SpecInfo表，更新记录
        public void UpdateDB(int ID)//this.Type
        {
            int m_Type = 0;
            switch (this.NominalType)
            {
                case NominalType.SingleSymm:
                    m_Type = 0;
                    break;
                case NominalType.DoubleSymm:
                    m_Type = 1;
                    break;
                case NominalType.SingleNoSymm:
                    m_Type = 2;
                    break;
                case NominalType.SingleWidthSymm:
                    m_Type = 3;
                    break;
            }

            int m_MidType = 0;
            switch (this.FindMidType)
            {
                case CenterType.None:
                    m_MidType = 0;
                    break;
                case CenterType.Lowest:
                    m_MidType = 1;
                    break;
                case CenterType.Highest:
                    m_MidType = 2;
                    break;
            }

            String str = "Update SpecInfo  Set " +
                                "NominalType= " + m_Type + "," +
                                "RimHeight=" + this.RimHeight + "," +
                                "RimTol=" + this.RimHeightTol + "," +
                                "TotalWidth=" + this.TotalWidth + "," +
                                "WidthTol=" + this.WidthTol + "," +
                                "TextNo='" + this.DieNo + "'," +
                                "ShoulderWidth=" + this.ShoulderWidth + "," +
                                "ShoulderWidthTol=" + this.ShoulderWidthTol + "," +
                                "ShoulderHeight=" + this.ShoulderHeight + "," +
                                "ShoulderHeightTol=" + this.ShoulderHeightTol + "," +
                                "jiao='" + this.Jiao + "'," +
                                "DateTimePicker='" + this.DateTimePicker + "'," +
                                "AreaTol=" + this.AreaDiff + "," +
                                "GJD='" + NominalDataHanding.DBStr(this.MyKeyPoints) + "'," +
                                "ResultPercent=" + this.OKPercent + "," +
                                "FindMidType=" + m_MidType + " " +
                                "Where ID=" + ID + " ";
            try
            {
                using (clsComDB db = clsDBManager.CreateComDB())
                {
                    db.Open();
                    db.ExecSql(str);
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message.ToString());
            }

        }

        //'数据库SpecInfo表，更新记录
        public void UpdateDBWeight(int ID)//this.Type
        {
            int m_Type = 0;
            switch (this.NominalType)
            {
                case NominalType.SingleSymm:
                    m_Type = 0;
                    break;
                case NominalType.DoubleSymm:
                    m_Type = 1;
                    break;
                case NominalType.SingleNoSymm:
                    m_Type = 2;
                    break;
                case NominalType.SingleWidthSymm:
                    m_Type = 3;
                    break;
            }

            int m_MidType = 0;
            switch (this.FindMidType)
            {
                case CenterType.None:
                    m_MidType = 0;
                    break;
                case CenterType.Lowest:
                    m_MidType = 1;
                    break;
                case CenterType.Highest:
                    m_MidType = 2;
                    break;
            }

            String str = "Update SpecInfo  Set " +
                                "NominalType= " + m_Type + "," +
                                "WeightSD='" + this.WeightSD.ToString() + "'," +
                                "WeightTolUp='" + this.WeightTolUp.ToString() + "'," +
                                "WeightTolDown='" + this.WeightTolDown.ToString() + "'," +
                                "WeightAlmUp='" + this.WeightAlmUp.ToString() + "'," +
                                "WeightAlmDown='" + this.WeightAlmDown.ToString() + "'," +
                                "FindMidType=" + m_MidType + " " +
                                "Where ID=" + ID + " ";
            try
            {
                //using (clsComDB db = clsDBManager.CreateComDB())
                //{
                //    db.Open();
                //    db.ExecSql(str);
                //}
                //clsAccess db = new clsAccess();
                //db.Open(Application.StartupPath + "\\WeightDataBase.mdb");
                //db.ExecSql(str);
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message.ToString());
            }

        }
        //'从数据库中删除记录
        public void DelDB(int ID)
        {
            String str = "Delete From SpecInfo Where ID = " + ID;
            try
            {
                using (clsComDB db = clsDBManager.CreateComDB())
                {
                    db.Open();
                    db.ExecSql(str);
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message.ToString());
            }

        }
        public void DelDB1(int ID)
        {
            String str = "Delete From SpecInfo Where ID = " + ID;
            try
            {
                //clsAccess db = new clsAccess();
                //db.Open(Application.StartupPath + "\\WeightDataBase.mdb");
                //db.ExecSql(str);
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message.ToString());
            }

        }
        #endregion

        public float WeightTolUp = 0;

        public float WeightTolDown = 0;

        public float WeightSD = 0;

        public float WeightAlmUp = 0;

        public float WeightAlmDown = 0;


    }

    public class NominalDataHanding
    {
        //public static List<PointData> Remove(List<PointData> ProfilePFs, Single value)
        //{
        //    //'List.Remove(value)
        //    for (int i = 0; i < ProfilePFs.Count - 1; i++)
        //    {
        //        if (value == ProfilePFs[i].X)
        //        {
        //            ProfilePFs.RemoveAt(i);
        //            break;
        //        }
        //    }
        //    return ProfilePFs;
        //} //'Remove

        //'将PointData的数据转换为以*分割的字符串
        public static String DBStr(NominalPointDataList ProfilePFs)
        {
            String str = "";
            for (int i = 0; i < ProfilePFs.Count; i++)
            {
                NominalPointData p = (NominalPointData)ProfilePFs[i];
                //str = str + p.DBStr() + "*";
                str = str + PDToDBStr(p, true) + "*";
            }
            return str;
        }
        //将点坐标转换为以#分割的字符串
        public static string PDToDBStr(NominalPointData m_Point, Boolean IsNominal)
        {
            string str = "";
            if (IsNominal == true)
            {
                str = m_Point.Point.X.ToString() + "#" + m_Point.Point.Y.ToString() + "#" + m_Point.XOffset.ToString() + "#" + m_Point.YOffset.ToString() + "#" + m_Point.IsCheck.ToString() + "#" + m_Point.IsCheckDuiChen.ToString() + "#" + m_Point.TolDuiChenThick + "#" + m_Point.IsTaiJian.ToString() + "#" + m_Point.YOffset2.ToString();
            }
            else
                str = m_Point.Point.X.ToString() + "#" + m_Point.Point.Y.ToString() + "#" + m_Point.XOffset.ToString() + "#" + m_Point.YOffset.ToString();
            return str;
        }
        //'以*为分隔符,将字符串分割为ProfilePFColl的List
        public static NominalPointDataList DBStr(String Str)
        {
            NominalPointDataList m_ProfilePFs = new NominalPointDataList();
            String[] s = Str.Split('*');
            if (s == null)
                return m_ProfilePFs;

            for (int i = 0; i < s.Length - 1; i++)
            {
                if (s[i].Length > 0)
                {
                    NominalPointData p = PointDataFromStr(s[i], true);
                    m_ProfilePFs.Add(p);

                }
            }
            m_ProfilePFs.AscSortForX();
            return m_ProfilePFs;
        }
        //'以#为分隔符,将字符串转换为ProfilePF的各个字段值
        public static NominalPointData PointDataFromStr(string str, Boolean IsNominal)
        {
            NominalPointData m_Point;
            string[] s = str.Split('#');
            if (s == null)
            {
                m_Point = new NominalPointData(new PointF(0, 0), 0, 0);
            }
            else if (s.Length > 3)
            {
                if (IsNominal == true)
                {
                    m_Point = new NominalPointData(new PointF(float.Parse(s[0]), float.Parse(s[1])), float.Parse(s[2]), float.Parse(s[3]), Boolean.Parse(s[4]), Boolean.Parse(s[5]), float.Parse(s[6]));
                    try
                    {
                        if (s.Length > 8)
                            m_Point.IsTaiJian = Boolean.Parse(s[7]);
                        if (s.Length > 9)
                            m_Point.YOffset2 = float.Parse(s[8]);
                    }
                    catch (Exception ex)
                    {
                        //MessageBox.Show(ex.Message);

                    }
                }
                else
                {
                    m_Point = new NominalPointData(new PointF(float.Parse(s[0]), float.Parse(s[1])), float.Parse(s[2]), float.Parse(s[3]));
                }
            }
            else
            {
                m_Point = new NominalPointData(new PointF(0, 0), 0, 0);
            }
            return m_Point;

        }

        //从数据库中加载所有ProfileType类型的规格
        //public static List<ClsMyNominal> GetDB(int NominalType)//ProfileType
        //{
        //    List<ClsMyNominal> m_NominalDatas = new List<ClsMyNominal>();
        //    String str = "select * from SpecInfo where NominalType=" + NominalType;
        //    using (clsComDB db = clsDBManager.CreateComDB())
        //    {
        //        db.Open();
        //        DataTable dt = db.GetDataTable(str);
        //        for (int i = 0; i < dt.Rows.Count; i++)
        //        {
        //            ClsMyNominal p = new ClsMyNominal(dt.Rows[i]);
        //            m_NominalDatas.Add(p);
        //        }
        //        db.Close();
        //    }
        //    return m_NominalDatas;
        //}

        //按规格加名称检索
        public static List<ClsMyNominal> GetDB(int NominalType, string strName)
        {
            List<ClsMyNominal> m_NominalDatas = new List<ClsMyNominal>();
            String str = "select * from SpecInfo where NominalType=" + NominalType;
            if (strName != "" && strName != null)
            {
                str += string.Format(" and ProfileName like '%{0}%'", strName);
            }
            str = str + " order by ProfileName";
            using (clsComDB db = clsDBManager.CreateComDB())
            {
                //clsAccess db1 = new clsAccess();
                //db1.Open(Application.StartupPath + "\\WeightDataBase.mdb");
                db.Open();
                DataTable dt = db.GetDataTable(str);
                //DataTable dt1 = db1.GetDataTable(str);
                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    ClsMyNominal p;

                    p = new ClsMyNominal(dt.Rows[i]);

                    m_NominalDatas.Add(p);

                }


                db.Close();
                //db1.Close();
            }
            return m_NominalDatas;
        }
        ////按规格加名称检索
        //public static List<ClsMyNominal> GetDB(int NominalType, string strName)
        //{
        //    List<ClsMyNominal> m_NominalDatas = new List<ClsMyNominal>();
        //    String str = "select * from SpecInfo where NominalType=" + NominalType;
        //    if (strName != "" && strName != null)
        //    {
        //        str += string.Format(" and ProfileName like '%{0}%'", strName);
        //    }
        //    str = str + " order by ProfileName";
        //    using (clsComDB db = clsDBManager.CreateComDB())
        //    {
        //        db.Open();
        //        DataTable dt = db.GetDataTable(str);
        //        for (int i = 0; i < dt.Rows.Count; i++)
        //        {
        //            ClsMyNominal p = new ClsMyNominal(dt.Rows[i]);
        //            m_NominalDatas.Add(p);
        //        }
        //        db.Close();
        //    }
        //    return m_NominalDatas;
        //}
        //按TypeCode(DieNo)检索
        //public static List<ClsMyNominal> GetDB(string strCode)
        //{
        //    List<ClsMyNominal> m_NominalDatas = new List<ClsMyNominal>();
        //    String str = "select * from SpecInfo where TextNo='" + strCode.Trim() + "'";
        //    str = str + " order by ProfileName";
        //    using (clsComDB db = clsDBManager.CreateComDB())
        //    {
        //        db.Open();
        //        DataTable dt = db.GetDataTable(str);
        //        for (int i = 0; i < dt.Rows.Count; i++)
        //        {
        //            ClsMyNominal p = new ClsMyNominal(dt.Rows[i]);
        //            m_NominalDatas.Add(p);
        //        }
        //        db.Close();
        //    }
        //    return m_NominalDatas;
        //}

        /// <summary>
        /// 计算关键点是否合格，并判定制品是否合格
        /// </summary>
        /// <param name="NominalKeyPoints">关键点标准</param>
        /// <param name="MeasuringKeyPoints">关键点数据</param>
        /// <param name="OKPercent">判定百分比</param>
        /// <param name="AlarmType1">是否使用Type1判定</param>
        /// <param name="AlarmType2">是否使用Type2判定</param>
        /// <param name="AlarmType3">是否使用Type3判定</param>
        /// <param name="MyAlarmPointsList">返回关键点判定信息</param>
        /// <returns></returns>
        public static MeasureResult JugeResult(NominalPointDataList NominalKeyPoints,
            PointDataList MeasuringKeyPoints, out List<MyAlarmPoints> MyAlarmPointsList, JugePar ParData, int OKPercent)
        {
            MeasureResult m_MeasureResult = new MeasureResult();
            int totalNum = NominalKeyPoints.Count;
            int BadCount = 0;
            int CheckCount = 0;
            MyAlarmPoints TempAlarmPoints = new MyAlarmPoints();
            PointData TempDiffKeyPoint;
            List<MyAlarmPoints> m_MyAlarmPoints = new List<MyAlarmPoints>();
            bool NoDuiChen = false;
            if (NominalKeyPoints.Count == MeasuringKeyPoints.Count)
            {
                //比较厚度
                for (int i = 1; i < NominalKeyPoints.Count - 1; i++)
                {
                    if (NominalKeyPoints[i].Point.X != 0)
                    {//不是中心点
                        if (NominalKeyPoints[i].IsCheck == true)
                        {
                            if (Math.Abs(MeasuringKeyPoints[i].Point.Y - NominalKeyPoints[i].Point.Y) > NominalKeyPoints[i].YOffset + 0.001f
                                || Math.Abs(MeasuringKeyPoints[i].Point.X - NominalKeyPoints[i].Point.X) > NominalKeyPoints[i].XOffset + 0.02f)
                            {//检查厚度、宽度 超出公差
                                TempDiffKeyPoint = new PointData(new PointF(Math.Abs(MeasuringKeyPoints[i].Point.X - NominalKeyPoints[i].Point.X), Math.Abs(MeasuringKeyPoints[i].Point.Y - NominalKeyPoints[i].Point.Y)));
                                //TempDiffKeyPoint.Point.Y=Math.Abs(MeasuringKeyPoints[i].Point.Y - NominalKeyPoints[i].Point.Y);
                                TempAlarmPoints.NominalKeyPoint = (PointData)NominalKeyPoints[i];
                                TempAlarmPoints.MeasuringKeyPoint = MeasuringKeyPoints[i];
                                TempAlarmPoints.DiffKeyPoint = TempDiffKeyPoint;
                                m_MyAlarmPoints.Add(TempAlarmPoints);
                                BadCount++;
                            }
                            if (i < NominalKeyPoints.Count / 2 && NominalKeyPoints[i].IsCheckDuiChen)
                            {
                                if (Math.Abs(MeasuringKeyPoints[i].Point.Y - MeasuringKeyPoints[NominalKeyPoints.Count - i - 1].Point.Y) > NominalKeyPoints[i].TolDuiChenThick)
                                {
                                    NoDuiChen = true;
                                }
                            }
                            CheckCount++;

                        }
                    }
                }
                //比较宽度
                for (int i = 0; i < NominalKeyPoints.Count; i++)
                {
                    if (Math.Abs(MeasuringKeyPoints[i].Point.X - NominalKeyPoints[i].Point.X) > NominalKeyPoints[i].XOffset)
                    {//检查厚度超出公差

                    }
                }
            }
            MyAlarmPointsList = m_MyAlarmPoints;
            m_MeasureResult.OkPoints = totalNum - BadCount;
            m_MeasureResult.TotalPoints = totalNum;
            m_MeasureResult.FPercent = float.Parse(string.Format("{0:f2}", (float)(m_MeasureResult.OkPoints) / (float)totalNum)) * 100;
            m_MeasureResult.BResult = true;
            m_MeasureResult.AlarmType = 0;

            if (BadCount > 0)
            {
                m_MeasureResult.AlarmType = 1;
            }

            if (ParData.AlarmType1)//任意1个位置超出0.5公差
            {
                //if (BadCount > 0)
                if (BadCount == 1)
                {
                    for (int k = 0; k < m_MyAlarmPoints.Count; k++)
                    {
                        if (m_MyAlarmPoints[k].DiffKeyPoint.Point.Y > ParData.ThickTolTol)
                        {
                            m_MeasureResult.BResult = false;
                            m_MeasureResult.AlarmType += 2;

                            break;
                        }
                    }
                }
            }
            if (ParData.AlarmType2)//允许2-4个位置超出公差
            {
                if (CheckCount <= 10)
                {
                    if (BadCount > 2)
                    {
                        m_MeasureResult.BResult = false;
                        m_MeasureResult.AlarmType += 4;
                    }
                }
                if (CheckCount > 10 && CheckCount <= 20)
                {
                    if (BadCount > 3)
                    {
                        m_MeasureResult.BResult = false;
                        m_MeasureResult.AlarmType += 4;
                    }
                }
                if (CheckCount > 20)
                {
                    if (BadCount > 4)
                    {
                        m_MeasureResult.BResult = false;
                        m_MeasureResult.AlarmType += 4;
                    }
                }
            }
            if (ParData.AlarmType3)//百分比的位置超出公差
            {
                if (m_MeasureResult.FPercent > OKPercent)
                {
                    m_MeasureResult.BResult = true;
                    //m_MeasureResult.AlarmType = 0;
                }
                else
                {
                    m_MeasureResult.BResult = false;
                    m_MeasureResult.AlarmType += 8;
                }
            }
            if (ParData.AlarmType4 && NoDuiChen)//不对称
            {
                m_MeasureResult.BResult = false;
                m_MeasureResult.AlarmType += 16;
            }

            return m_MeasureResult;
        }
    }
}
